<template>
	<div class="boxmain">
		<h3>生成成绩分析<el-button type="primary" @click="sxjd">刷新进度</el-button></h3> 
		<el-table
		    :data="tableData"
		    border
		    style="width: 100%">
		    <el-table-column
		      prop="fileName"
		      label="报表级别"
		      width="180">
			  <template slot-scope="scope">
			    <span v-if="scope.row.type == 2">{{scope.row.fileName}}<div style="color: #86909C;">班级报表</div></span>
			    <span v-else-if="scope.row.type == 0">{{scope.row.fileName}}<div style="color: #86909C;">校级报表</div></span>
			    <span v-else-if="scope.row.type == 1">{{scope.row.fileName}}<div style="color: #86909C;">联考报表</div></span>
			  </template>
		    </el-table-column>
		    <el-table-column
		      label="学校"
		      width="180">
			  <template slot-scope="scope">
				  {{scope.row.schoolName?scope.row.schoolName:'--'}}
			  </template>
		    </el-table-column>
		    <el-table-column
		      prop="createTime"
		      label="生成时间">
		    </el-table-column>
			<el-table-column
			  label="状态">
			  <template slot-scope="scope">
			    <el-tag v-if="scope.row.status == 0" type="info">
					{{progresstotal!=0?progresstotal+'%':'100%'}}
				</el-tag>
			    <el-tag v-if="scope.row.status == 1" type="success">已完成</el-tag>
			  </template>
			</el-table-column>
			<el-table-column
			  label="操作">
			  <template slot-scope="scope">
				  <div style="display: flex; align-items: center;">
					  <el-button :disabled="scope.row.status == 0?true:false" @click="download(scope.row)" type="text" size="small">下载</el-button>
					  <el-button  @click="deleteExcel(scope.row)" type="text" size="small">删除</el-button> 
					  <el-button :disabled="scope.row.status == 1?true:false"  @click="downloadFile(scope.row,0)" type="text" size="small" v-if="!loadIshow">生成</el-button> 
					  	<div style="display: flex;align-items: center;font-size:12px;margin-left:15px;" v-if="loadIshow">
					  		<img style="width: 15px;margin-right: 4px;" src="../../../assets/image/loading1.gif" alt="">
					  		生成中
					  	</div>
					  <el-button :disabled="scope.row.status == 1?false:true" @click="downloadFile(scope.row,1)" type="text" size="small" v-if="!loadIshow">重新生成</el-button> 
				  </div>
				  
				</template>
			</el-table-column>
		  </el-table>
		  
	</div>
</template>

<script>
	import {analysisStuScoreExcelDownloadPage,analysisStuScoreExcelDownloadDelete,downloadExcel,downloadprogressget} from '../../../api/scoreapi.js'
	export default{
		data(){
			return {
				tableData:[],
				pageIndex:1,
				pageSize:20,
				tableheight:'',
				loadIshow:false,
				progresstotal:''
			}
		},
		mounted() {
			this.getScreenHeight();
			//在窗口大小变化时更新屏幕高度，可以监听window的resize事件
			window.addEventListener('resize', this.getScreenHeight);
			let newtitle = decodeURIComponent(this.$route.query.con)
			this.titlecontent = JSON.parse(newtitle)
			this.getExcelList()
			localStorage.setItem('menuState', 2);
			this.downloadprogressgetclick()
			
		},
		methods:{
			//刷新进度
			downloadprogressgetclick(){
				let paranms={
					navId:this.titlecontent.n5Id
				}
				downloadprogressget(paranms).then(res=>{
					console.info(res)
					if(res.code==0){
						this.progresstotal= res.data.toFixed(2)
						console.info(this.progresstotal)
					}
					
				})
			},
			sxjd(){
				this.downloadprogressgetclick()
				this.getExcelList()
			},
			getScreenHeight() {
			  // 获取屏幕高度并更新data中的screenHeight
			  this.tableheight = parseInt(window.innerHeight-450);
			},
			getExcelList() {
			  let query = {
			    pageIndex: this.pageIndex,
			    pageSize: this.pageSize,
			    navId: this.titlecontent.n5Id,
			  }
			  analysisStuScoreExcelDownloadPage(query).then(res => {
			    // console.log(res);
				if(res.code==0){
					this.tableData = res.data.list
				}else{
					this.$message({
						type: 'error',
						message: res.msg
					}); 
				}
			  })
			},
			download(e) {
				if(e.filePath){
					window.open(e.filePath, '_blank');
				}else{
					this.$message({
						type: 'error',
						message: '生成下载表失败'
					}); 
				}
			},
			deleteExcel(e) {
			  let this_ = this
			  this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
			    confirmButtonText: '确定',
			    cancelButtonText: '取消',
			    type: 'warning'
			  }).then(() => {
			    analysisStuScoreExcelDownloadDelete({id: e.id}).then(res => {
			      if (res.code == 0) {
			        this_.$message.success('删除成功');
			        let query = {
			          pageIndex: this_.pageIndex,
			          pageSize: this_.pageSize,
			          navId: this_.titlecontent.n5Id,
			        }
			        this_.getExcelList(query)
			      } else {
			        this_.$message.error(res.msg);
			      }
			    })
			  }).catch(() => {
			        
			  })
			},
			downloadFile(con,id) {
				this.loadIshow = true
			    let obj = {
			        navId: this.titlecontent.n5Id,
			        type: 1,
			        genExcel: 1,
			        regen: id,//0开始 1重新
			        isCover: 1,
			        isLine:1,
			        lineType: 0
			    }
			    downloadExcel(obj).then(res => {
			        if (res.code == 0) {
			            this.$message.success('成绩生成成功，请前往下载成绩页面下载')
						this.loadIshow = false
			        }
			    })
			},
		}
	}
</script>

<style scoped>
	.boxmain h3{
		padding:20px 0;
		font-size:18px;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.boxmain{
		padding:30px;
	}
</style>